#ifndef ALP_DICT_H
#define ALP_DICT_H

/**
 * @file	alp_dict.h
 * @brief	Dictionary
 */

#include "alp_type.h"
#include "alp_list.h"


/** Abstract type for dictionaries. */
typedef struct alp_dict_t alp_dict_t;

/** Abstract type for scanning dictionaries. */
typedef struct alp_dict_index_t alp_dict_index_t;

typedef struct entry_t entry_t;


struct alp_dict_t
{
	size_t count;
	alp_list_t **buckets;
};

struct entry_t
{
	void *key;
	size_t klen;
	void *val;
};

struct alp_dict_index_t
{
	alp_dict_t *dict;
	size_t bucket_id;
	alp_list_index_t *li;
};

/**
 * Create a new dictionary.
 * @param newdict	the dictionary just created
 * @param pool		the pool to allocate a dictionary out of
 * @return 0 for OK
 */
alp_status_t alp_dict_create(alp_dict_t **newdict);

/**
 * Lookup the value for a given key.
 * @param dict	the dictionary
 * @param key	the key
 * @param klen	the length of the key
 * @return the value associated with the key
 */
void * alp_dict_get(alp_dict_t *dict, const void *key, size_t klen);

/**
 * Set the value associated with a given key.
 * @param dict	the dictionary
 * @param key	the key
 * @param klen	the length of the key
 * @param val	the value associated with the key
 * @return 0 for OK
 */
alp_status_t alp_dict_set(alp_dict_t *dict, const void *key, size_t klen, void *val);

/**
 * Get the first element of a dictionary.
 * @param dict	the dictionary
 * @param pool	the pool to allocate an index out of
 * @return the index for iteration
 */
alp_dict_index_t * alp_dict_first(alp_dict_t *dict);

/**
 * Get the next element.
 * @param di	the index for iterating the dictionary
 * @return the index for next iteration
 */
alp_dict_index_t * alp_dict_next(alp_dict_index_t *di);

/**
 * Get the current entry.
 * @param di	the index for iterating the dictionary
 * @param key	the key for the index
 * @param klen	the length of the key
 * @param val	the value for the index
 */
void alp_dict_this(alp_dict_index_t *di, const void **key, size_t *klen, void **val);

void alp_dict_destroy(alp_dict_t * dict);
#endif
